﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;

public class C_mineCubeInput : MonoBehaviour {

    public Input_Type OnClickType
    {
        get;
        private set;
    }

    public Action<Input_Type> OnClickChange;
    public Action OnShortPress;
    public Action OnLongPress;

    
    //是否在该对象上
    private bool isActivity = false;
    //记录按下时的时间节点
    private float LastClickTime;

    private void OnMouseDown()
    {
        isActivity = true;
        changeClickType(Input_Type.OnDown);
    }

    private void OnMouseUp()
    {
        if (isActivity)
        {
            changeClickType(Input_Type.OnUp);
            isActivity = false;
        }
    }

    private void OnMouseExit()
    {
        isActivity = false;
        changeClickType(Input_Type.None);
    }

    //private void OnMouseEnter()
    //{
    //    isActivity = true;
    //}

    private void changeClickType(Input_Type input_Type) {
        if (M_GameManager.Instance.mineCommonData.isLockInput) return;

        if (OnClickType != input_Type) {
            OnClickChange?.Invoke(input_Type);
            switch (input_Type)
            {
                case Input_Type.None:
                    LastClickTime = 0;
                    break;
                case Input_Type.OnDown:
                    LastClickTime = Time.time;
                    break;
                case Input_Type.OnUp:
                    if (LastClickTime != 0) {
                        var clickTime = Time.time - LastClickTime;
                        if (clickTime >= 0.5f)
                            OnLongPress?.Invoke();
                        else
                            OnShortPress?.Invoke();
                    }
                    break;
            }
        }
        OnClickType = input_Type;
    }
}

public enum Input_Type {
    None,
    OnDown,
    OnUp
}
